package com.lightsh.handwritten;

import java.util.ArrayList;

import com.lightsh.common.BottomBarLayout;
import com.lightsh.common.NoteItem;
import com.lightsh.common.NoteItemView;
import com.lightsh.db.DbDataHelper;
import com.lightsh.db.MyDbHelper;
import com.lightsh.list.AbstractListItem;
import com.lightsh.list.CommonListAdapter;
import com.lightsh.list.OnItemSelectListener;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;

public class NoteListActivity extends Activity implements AdapterView.OnItemClickListener,
OnClickListener{

	private ListView mListNote;
	private BaseAdapter mAdapter;
	private ArrayList<NoteItem> mDataList = new ArrayList<NoteItem>();
	
	private BottomBarLayout mBottomBar;
	
	private static final int REQUEST_CODE_NEW = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note_list_layout);
		mListNote = (ListView) findViewById(R.id.noteList);
		mListNote.setOnItemClickListener(this);
		initAdapter();
		getNoteList();
		initBottomBar();
	}

	private void initAdapter(){
		mAdapter = new CommonListAdapter(this, mDataList, NoteItemView.class, new OnItemSelectListener() {
			
			@Override
			public void onItemSelect(int index, boolean bSelected) {
				// FIXME 处理选中事件, 能优化?
				mDataList.get(index).setSelected(bSelected);
				checkShowBottomBar();
			}
		});
		mListNote.setAdapter(mAdapter);
	}
	private void getNoteList(){
		DbDataHelper.getAllNotesList(this, mDataList);
		mAdapter.notifyDataSetChanged();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		if(mBottomBar.isBottomBarVisible()){
			setAllItemSelect(false);
			mBottomBar.hideBottomBar();
			return;
		}
		super.onBackPressed();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		startEditActivity(mDataList.get(position).getId());
	}
	private void startEditActivity(long position){
		Intent intent = new Intent(this, HandwrittingActivity.class);
		if(position != -1){
			intent.putExtra(HandwrittingActivity.EXTRA_NOTE_ID, position);
		}
		startActivityForResult(intent, REQUEST_CODE_NEW);
	}
	private void startPaintActivity(long id){
		Intent intent = new Intent(this, FingerPaintActivity.class);
		if(id != -1){
			intent.putExtra(FingerPaintActivity.EXTRA_PAINT_ID, id);
		}
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.newLayout:
			startEditActivity(-1);
			break;
		}
		
	}
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == Activity.RESULT_OK){
			switch (requestCode) {
			case REQUEST_CODE_NEW:
				handleNoteChange();
				break;

			default:
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void handleNoteChange(){
		mDataList.clear();
		getNoteList();
		checkShowBottomBar();
	}
	
	private void initBottomBar(){
		mBottomBar = new BottomBarLayout(this);
		mBottomBar.init(findViewById(R.id.bottomBarLayout));
		mBottomBar.updateBottomBarListener(mOnBottomBtnClickListener);
	}
	private void checkShowBottomBar(){
		int selectCount = 0;
		for(AbstractListItem item : mDataList){
			if(item.isSelected()){
				selectCount++;
			}
		}
		if(selectCount > 0){
			mBottomBar.setShowBottomBar(selectCount == mDataList.size());
		}else{
			mBottomBar.hideBottomBar();
		}
	}
	
	private OnClickListener mOnBottomBtnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.btnLeft:{
				deleteSelectedItem();
			}
				break;
			case R.id.btnRight:{
				Button button = (Button) v;
				if(getString(R.string.selectAll).equals(button.getText())){
					setAllItemSelect(true);
					mBottomBar.setShowBottomBar(true);
				}else{
					setAllItemSelect(false);
					mBottomBar.hideBottomBar();
				}
			}
				 break;
			}
			
		}
	};
	private void deleteSelectedItem(){
		MyDbHelper db = new MyDbHelper(this);
		boolean bChange = false;
		try{
			final int count = mDataList.size();
			NoteItem item;
			for(int i = count -1; i >= 0; --i){
				item = mDataList.get(i);
				if(item.isSelected()){
					boolean bSuccess = db.deleteNoteDataAndCharacters(item.getId());
					if(bSuccess){
						mDataList.remove(i);
						bChange = true;
					}
				}
			}
		}finally{
			if(db != null){
				db.close();
			}
		}
		if(bChange){
			mAdapter.notifyDataSetChanged();
			checkShowBottomBar();
		}
	}
	private void setAllItemSelect(boolean bSelect){
		for(AbstractListItem item : mDataList){
			if(item.isSelected() != bSelect){
				item.setSelected(bSelect);
			}
		}
		mAdapter.notifyDataSetChanged();
	}
}
